/*********************************************************************
 *    Project Name : tscp-platform
 *    Package Name : com.tscp.platform.service.util
 *    Create  Time : 2021/5/10 16:21
 *    =============================================================
 *    (C) Copyright Trustcro Information Technology Co., Ltd.
 *          Corporation 2018-2021 All Rights Reserved.
 *
 *	  【注意： 本内容仅限于长沙信立诚医药科技有限公司内部使用，禁止转发】
 **********************************************************************/
package com.trust.pams.service.util;


import com.alibaba.excel.EasyExcel;

import com.alibaba.fastjson.JSON;

import com.trust.pams.domain.PamsUser;
import com.trust.pams.dto.PamsClientDetailsDto;
import com.trust.pams.dto.PamsClientDto;
import com.trust.pams.dto.PamsMilestoneDto;
import com.trust.pams.dto.PamsQuoteDto;
import com.trust.pams.model.easyexcel.PamsUserExcel;
import com.trust.pams.util.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Project : tscp-platform
 * @Author : Chasonx<zc.x>
 * @CreateTime : 2021/5/10 16:21
 * @Desc :
 * @Version : 1.0
 */
public class TrutsPamsImportUtil {



    public static List<PamsQuoteDto> readQupte(String filePath) {
        List<PamsQuoteDto> data = new ArrayList<PamsQuoteDto>();
        CustomeEasyExcelListener listener = new CustomeEasyExcelListener();
        EasyExcel.read(filePath, listener).sheet(0).doRead();

        List<HashMap> listRows = new ArrayList<HashMap>();

        Map<String, HashMap> rowData = listener.getMap();
        List<String> listColumn = listener.getListColumn();
        List<HashMap> fdata = rowData.keySet().stream().map(rowIdx -> {
            Map rowMap = rowData.get(rowIdx);
            Set<Integer> rowSet =  rowMap.keySet();
            HashMap<String,Object> row = new LinkedHashMap<>();
            AtomicInteger idx = new AtomicInteger(0);
            PamsQuoteDto pamsQuote = new PamsQuoteDto();
            rowSet.stream().forEach(r -> {
                row.put(listColumn.get(idx.get()), rowMap.get(r));
                idx.set(idx.get() + 1);
            });
            pamsQuote.setId(UUIDUtil.generateUUID());
            pamsQuote.setProjectGuid(UUIDUtil.generateUUID());
            if(DataUtil.isNotEmpty(row.get("方案名称"))){
                pamsQuote.setProjectName(row.get("方案名称").toString());
            }
            if(DataUtil.isNotEmpty(row.get("客户名称"))){
                pamsQuote.setClientName(row.get("客户名称").toString());
            }
            if(DataUtil.isNotEmpty(row.get("项目编号"))){
                pamsQuote.setProjectId(row.get("项目编号").toString());
            }
            pamsQuote.setRows(row);
            data.add(pamsQuote);
            return row;
        }).collect(Collectors.toList());
        return data;
    }


    public static List<PamsClientDto> readClient(String filePath) {
        List<PamsClientDto> data = new ArrayList<PamsClientDto>();
        List<PamsClientDetailsDto> pamsClientDetailsDtoList= new ArrayList<>();
        CustomeEasyExcelListener listener = new CustomeEasyExcelListener();
        EasyExcel.read(filePath, listener).sheet(0).doRead();

        CustomeEasyExcelListener listeners = new CustomeEasyExcelListener();
        EasyExcel.read(filePath, listeners).sheet(1).doRead();

        List<HashMap> listRows = new ArrayList<HashMap>();

        Map<String, HashMap> rowData = listener.getMap();
        List<String> listColumn = listener.getListColumn();
        Map<String, HashMap> rowDatas = listeners.getMap();
        List<String> listColumns = listeners.getListColumn();
        List<HashMap> fdatas = rowDatas.keySet().stream().map(rowIdx -> {
            Map rowMap = rowDatas.get(rowIdx);
            Set<Integer> rowSet =  rowMap.keySet();
            HashMap<String,Object> row = new LinkedHashMap<>();
            AtomicInteger idx = new AtomicInteger(0);
            PamsClientDetailsDto pamsClientDetailsDto = new PamsClientDetailsDto();
            rowSet.stream().forEach(r -> {
                row.put(listColumns.get(idx.get()), rowMap.get(r));
                idx.set(idx.get() + 1);
            });
            pamsClientDetailsDto.setClientName(row.get("客户名称").toString());
            pamsClientDetailsDto.setRows(row);
            pamsClientDetailsDtoList.add(pamsClientDetailsDto);
            return row;
        }).collect(Collectors.toList());
        List<HashMap> fdata = rowData.keySet().stream().map(rowIdx -> {
            Map rowMap = rowData.get(rowIdx);
            Set<Integer> rowSet =  rowMap.keySet();
            HashMap<String,Object> row = new LinkedHashMap<>();
            AtomicInteger idx = new AtomicInteger(0);
            PamsClientDto clientDto = new PamsClientDto();
            rowSet.stream().forEach(r -> {
                row.put(listColumn.get(idx.get()), rowMap.get(r));
                idx.set(idx.get() + 1);
            });
            clientDto.setClientName(row.get("客户名称").toString());
            clientDto.setClientBasic(row);
            for(PamsClientDetailsDto detailsDto:pamsClientDetailsDtoList){
                if(row.get("客户名称").toString().equals(detailsDto.getClientName())){
                    clientDto.setClientDetails(detailsDto.getRows());
                }
            }
            data.add(clientDto);
            return row;
        }).collect(Collectors.toList());
        return data;
    }



    public static List<PamsUser> readUser(String filePath){
        ExcelListener<PamsUserExcel> listener = new ExcelListener<PamsUserExcel>();
        EasyExcelUtil.analysisExcel(filePath, listener, PamsUserExcel.class, 0, null, 2);
        List<PamsUserExcel> objList=listener.getDatas();
        List<PamsUser> pamsUserList = new ArrayList<PamsUser>();
        for(PamsUserExcel pamsUserExcel:objList){
            PamsUser pamsUser= new PamsUser();
            pamsUser.setId(UUIDUtil.generateUUID());
            pamsUser.setName(pamsUserExcel.getName());
            pamsUser.setLoginName(pamsUserExcel.getLoginName());
            pamsUser.setStaffCode(pamsUserExcel.getStaffCode());
            if(pamsUserExcel.getStaffState().equals("正式")){
                pamsUser.setStaffState(1);
            }else if(pamsUserExcel.getStaffState().equals("试用")){
                pamsUser.setStaffState(2);
            }else if(pamsUserExcel.getStaffState().equals("兼职")){
                pamsUser.setStaffState(3);
            }

            pamsUser.setSection(pamsUserExcel.getSection());
            pamsUser.setKeyPost(pamsUserExcel.getKeyPost());
            pamsUser.setJobLevel(pamsUserExcel.getJobLevel());
            pamsUser.setMailbox(pamsUserExcel.getMailbox());
            if(DataUtil.isNotEmpty(pamsUserExcel.getSex())){
                if(pamsUserExcel.getSex().equals("男")){
                    pamsUser.setSex(1);
                }else if(pamsUserExcel.getSex().equals("女")){
                    pamsUser.setSex(2);
                }
            } else{
                pamsUser.setSex(0);
            }

            pamsUserList.add(pamsUser);
        }
        return pamsUserList;
    }

    public static List<PamsMilestoneDto> readMilestone(String filePath) {
        List<PamsMilestoneDto> data = new ArrayList<PamsMilestoneDto>();
        CustomeEasyExcelListeners listener = new CustomeEasyExcelListeners();
        EasyExcel.read(filePath, listener).sheet(0).doRead();
        List<HashMap> listRows = new ArrayList<HashMap>();
        Map<String, HashMap> rowData = listener.getMap();
        List<String> listColumn = listener.getListColumn();
        List<HashMap> fdata = rowData.keySet().stream().map(rowIdx -> {
            Map rowMap = rowData.get(rowIdx);
            Set<Integer> rowSet =  rowMap.keySet();
            HashMap<String,Object> row = new LinkedHashMap<>();
            AtomicInteger idx = new AtomicInteger(0);
            PamsMilestoneDto pamsMilestoneDto = new PamsMilestoneDto();
            rowSet.stream().forEach(r -> {
                row.put(listColumn.get(idx.get()), rowMap.get(r));
                idx.set(idx.get() + 1);
            });
            pamsMilestoneDto.setId(UUIDUtil.generateUUID());
            if(DataUtil.isNotEmpty(row.get("项目编号"))){
                pamsMilestoneDto.setProjectId(row.get("项目编号").toString());
            }
            pamsMilestoneDto.setRows(row);
            data.add(pamsMilestoneDto);
            return row;
        }).collect(Collectors.toList());
        return data;
    }

    public static void main(String[] args) {
        String path = "C:/Users/Min Zou/Documents/WeChat Files/wxid_5mwy8np3hlf022/FileStorage/File/2021-07/报价数据导出表.xlsx";
        List<PamsQuoteDto> data = new ArrayList<>();
        CustomeEasyExcelListener listener = new CustomeEasyExcelListener();
        EasyExcel.read(path, listener).doReadAllSync();
        List<HashMap> listRows = new ArrayList<HashMap>();

        Map<String, HashMap> rowData = listener.getMap();
        List<String> listColumn = listener.getListColumn();

        List<HashMap> fdata = rowData.keySet().stream().map(rowIdx -> {
            Map rowMap = rowData.get(rowIdx);
            Set<Integer> rowSet =  rowMap.keySet();

            HashMap<String,Object> row = new LinkedHashMap<>();
            AtomicInteger idx = new AtomicInteger(0);
            rowSet.stream().forEach(r -> {
                row.put(listColumn.get(idx.get()), rowMap.get(r));
                idx.set(idx.get() + 1);
            });
            String cheshi = row.get("项目方案名称").toString();
            return row;
        }).collect(Collectors.toList());

        System.out.println(JSON.toJSONString(fdata));

        /*if (Kit.isEmpltyCollection(mapSheet)) {
            return null;
        }
        Map mapw = new HashMap<>();
        mapSheet.keySet().stream().forEach(key -> {
            if (key.equals(0)) {
                System.out.println(key);
            }
        });*/

    }
}
